﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Microsoft.MediaCenter.UI;
using System.IO;

namespace System.Runtime.CompilerServices
{
    public class ExtensionAttribute : Attribute
    { }
}

namespace MusicBrowser.LibraryManagement
{
     public static class MyExtension
    {
                 public static int SafeGetInt(this XmlDocument doc, string path)
        {
            return SafeGetInt(doc, path, 0);
        }

        public static int SafeGetInt(this XmlDocument doc, string path, int defaultInt)
        {
            XmlNode rvalNode = doc.SelectSingleNode(path);
            if (rvalNode != null && rvalNode.InnerText.Length > 0)
            {
                int rval;
                if (Int32.TryParse(rvalNode.InnerText, out rval))
                {
                    return rval;
                }

            }
            return defaultInt;
        }


        public static float SafeGetFloat(this XmlDocument doc, string path, float minValue, float maxValue)
        {
           return minValue;
        }


        public static string SafeGetString(this XmlDocument doc, string path)
        {
            return SafeGetString(doc, path, "");
        }

        public static string SafeGetString(this XmlDocument doc, string path, string defaultString)
        {
            XmlNode rvalNode = doc.SelectSingleNode(path);
            if (rvalNode != null && rvalNode.InnerText.Length > 0)
            {
                return rvalNode.InnerText;
            }
            return defaultString;
        }

        public static string SafeGetString(this XmlElement elem, string path)
        {
            return SafeGetString(elem, path, ""); 
        }

        public static string SafeGetString(this XmlElement elem, string path, string defaultString)
        {
            XmlNode rvalNode = elem.SelectSingleNode(path);
            if (rvalNode != null && rvalNode.InnerText.Length > 0)
            {
                return rvalNode.InnerText;
            }
            return defaultString;
        }
    }



    // This class is used for the xml caching stuff 
    public class CachedFolderItem : BaseFolderItem
    {
        FolderItemList parent; 

        // needed for mcml 
        public CachedFolderItem()
        { }

        // load the item from the element
        public CachedFolderItem(XmlElement elem, string folderHash, FolderItemList parent)
        {
            this.parent = parent;
            this.folderHash = folderHash;
            filename = elem.SafeGetString("Filename");
            title = elem.SafeGetString("Title");
            Description = elem.SafeGetString("Description");
            thumbHash = elem.SafeGetString("ThumbHash");
            isFolder = Boolean.Parse(elem.SafeGetString("IsFolder"));
            isSong = Boolean.Parse(elem.SafeGetString("IsSong"));
            isPlaylist = Boolean.Parse(elem.SafeGetString("IsPlaylist"));
            album = elem.SafeGetString("Album");
            artist = elem.SafeGetString("Artist");
            duration = elem.SafeGetString("Duration");
            maintitle = elem.SafeGetString("mainTitle");
            createdDate = DateTime.Parse(elem.SafeGetString("CreatedDate"));
            modifiedDate = DateTime.Parse(elem.SafeGetString("ModifiedDate"));
           
        }

        private string folderHash;

        private bool isSong;
        public override bool IsSong { get { return isSong; } }

        private bool isPlaylist;
        public override bool IsPlaylist { get { return isPlaylist; } }

        private bool isFolder;
        public override bool IsFolder { get { return isFolder; } }
        
        private string filename;
        public override string Filename { get {return filename;} }

        private string title;
        public override string Title { get { return title; } }
        
        private string thumbHash;
        public override string ThumbHash { get { return thumbHash; } }

       
        private string album;
        public override string Album { get { return album; } }

        private string artist;
        public override string Artist { get { return artist; } }

        private string duration;
        public override string Duration { get { return duration; } }

        private string maintitle;
        public override string mainTitle { get { return maintitle; } }
    

        private DateTime createdDate;
        public override DateTime CreatedDate { get { return createdDate; } }

        private DateTime modifiedDate;
        public override DateTime ModifiedDate { get { return modifiedDate; } }

        private string _thumbPath = null; 
        public override string ThumbPath
        {
            get
            {
                if (_thumbPath == null)
                {
                    string path = "";
                    if (!string.IsNullOrEmpty(ThumbHash))
                    {
                        path = System.IO.Path.Combine(Helper.AppCachePath, folderHash);
                        path = System.IO.Path.Combine(path, ThumbHash);
                        if (!File.Exists(path))
                        {
                            parent.DestroyCache();
                            var realItem = new FolderItem(Filename, IsFolder, Description);
                            path = realItem.ThumbPath;
                        }

                        _thumbPath = path;
                    }
                }
                return _thumbPath;
            }
            set
            {
                throw new NotImplementedException();
            }
        }

       

        

    }
}
